package com.kygl.teacher.dao.imp;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.kygl.reaserch.vo.ReaserchRegister;
import com.kygl.teacher.dao.ICourseDao;
import com.kygl.teacher.vo.CourseRegister;
import com.kygl.teacher.vo.Teacher;

public class CourseDaoImp extends HibernateDaoSupport implements ICourseDao{

	public boolean addCourseRegister(CourseRegister t) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().save(t);
			return true;

		} catch (RuntimeException re) {
			throw re;
		}
		
	}

	public boolean delCourseRegister(CourseRegister t) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().delete(t);
			return true;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public boolean delCourseRegisterById(int id) {
		// TODO Auto-generated method stub
		try {

			this.getHibernateTemplate().delete(this.getHibernateTemplate().get(
					"com.kygl.teacher.vo.CourseRegister", id));
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public List<CourseRegister> findAllCourseRegister() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		List<CourseRegister> list = this.getHibernateTemplate().loadAll(CourseRegister.class);
		// TODO Auto-generated method stub
		return list;
	}

	public CourseRegister findCourseRegisterById(int id) {
		// TODO Auto-generated method stub
		return (CourseRegister) this.getHibernateTemplate().get(
				"com.kygl.teacher.vo.CourseRegister", id);
	}

	@SuppressWarnings("unchecked")
	public List<CourseRegister> findCourseRegisterById(int deptId, int workId) {
		// TODO Auto-generated method stub
    final String hql = "from CourseRegister a where a.deptId="+deptId+"and a.workId="+workId;
		
		HibernateCallback hc = new HibernateCallback() {
			@SuppressWarnings("unchecked")
			public Object doInHibernate(org.hibernate.Session session)
					throws HibernateException, SQLException {
	
				Query query = session.createQuery(hql);
				List<CourseRegister> testlist = query.list();
				
				if (testlist.isEmpty()) {
					 System.out.println("no CourseRegister");
				}
				return testlist;
			}

		};
		List<CourseRegister> list= getHibernateTemplate().executeFind(hc);
		
		return list;
	}

	@SuppressWarnings("unchecked")
	public boolean findCourseRegisterByName(String name) {
		// TODO Auto-generated method stub
		String hql = "from CourseRegister a where a.name=?";

		List<CourseRegister> list = this.getHibernateTemplate().find(hql, name.trim().toLowerCase());

		if (list.isEmpty()) {
			return false;
		} else {
			return true;
		}
	}



	public boolean modifyCourseRegister(CourseRegister t) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().saveOrUpdate(t);
			return true;
		} catch (RuntimeException re) {

			throw re;
		}
	}
	
	public boolean addAuxid(int deptId, int workId, int iRand){
		try {
			String hql = "update CourseRegister a set a.auxid="+iRand+" " +
					" where a.deptId="+deptId+" and a.workId = "+workId;
			this.getHibernateTemplate().bulkUpdate(hql);
		} catch (DataAccessException e) {
			
			e.printStackTrace();
		}
		return true;
	} 
	
	@SuppressWarnings("unchecked")
	public List<CourseRegister> findByAuxId(int auxid){
		final String hql = "from CourseRegister a where a.auxid="+auxid;
		return (this.getHibernateTemplate().find(hql));
	}
	
	@SuppressWarnings("unchecked")
	public boolean findCourseRegisterByDetail(CourseRegister cr) {
		// TODO Auto-generated method stub
		String hql = "from CourseRegister a where a.coursename='" + cr.getCoursename()+"' and " +
				" a.cpbase="+ cr.getCpbase() + " and a.remark='"+cr.getRemark()+"'";
		System.out.println("hql:"+hql);
		List<CourseRegister> list = this.getHibernateTemplate().find(hql);

		if (list.isEmpty()) {
			return false;
		} else {
			return true;
		}
	}
}
